package com.umgsai.thread.thread67;

public class MyThread extends Thread {
	private Object lock;
	private String showChar;
	private int showNumberPosition;
	private int printCount = 0;
	volatile private static int addNumber = 1;
	public MyThread(Object lock, String showChar, int showNumberPosition) {
		super();
		this.lock = lock;
		this.showChar = showChar;
		this.showNumberPosition = showNumberPosition;
	}
	@Override
	public void run() {
		try {
			synchronized (lock) {
				while (true) {
					if (addNumber % 3 == showNumberPosition) {
						System.out.println(Thread.currentThread().getName() + "-" + addNumber + "-" + showChar);
						lock.notifyAll();
						addNumber++;
						printCount++;
						if (printCount == 3) {
							break;
						}
					}else {
						lock.wait();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Object lock = new Object();
		MyThread a = new MyThread(lock, "A", 1);
		MyThread b = new MyThread(lock, "B", 2);
		MyThread c = new MyThread(lock, "C", 0);
		a.start();
		b.start();
		c.start();
	}
}
